<!--
 * @Description: 产品详情对话框组件 - 展示产品的详细信息
 * @Author: lujq
 * @Date: 2024-12-21
 * @LastEditors: lujq
 * @LastEditTime: 2024-12-21 16:30:00
-->
<template>
  <el-dialog v-model="visible" title="产品详情" width="800px" :before-close="handleClose">
    <div v-if="product" class="product-detail">
      <!-- 基本信息 -->
      <el-card class="info-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>基本信息</span>
          </div>
        </template>
        <div class="product-basic">
          <div class="product-image">
            <el-image
              :src="product.image"
              :preview-src-list="[product.image]"
              fit="cover"
              style="width: 200px; height: 200px; border-radius: 8px"
            >
              <template #error>
                <div class="image-slot">
                  <el-icon><Picture /></el-icon>
                  <div>暂无图片</div>
                </div>
              </template>
            </el-image>
          </div>
          <div class="product-info">
            <div class="info-row">
              <label>产品编码：</label>
              <span>{{ product.productCode }}</span>
            </div>
            <div class="info-row">
              <label>产品名称：</label>
              <span>{{ product.name }}</span>
            </div>
            <div class="info-row">
              <label>产品规格：</label>
              <span>{{ product.specification }}</span>
            </div>
            <div class="info-row">
              <label>产品类别：</label>
              <el-tag :type="getCategoryTagType(product.category)">
                {{ getCategoryName(product.category) }}
              </el-tag>
            </div>
            <div class="info-row">
              <label>单价：</label>
              <span class="price">¥{{ product.price.toFixed(2) }}/米</span>
            </div>
            <div class="info-row">
              <label>库存：</label>
              <span :class="getStockClass(product.stock)">{{ product.stock }}米</span>
            </div>
            <div class="info-row">
              <label>销量：</label>
              <span>{{ product.sales }}米</span>
            </div>
            <div class="info-row">
              <label>状态：</label>
              <el-tag :type="product.status === 'active' ? 'success' : 'danger'">
                {{ product.status === 'active' ? '上架' : '下架' }}
              </el-tag>
            </div>
          </div>
        </div>
      </el-card>

      <!-- 详细描述 -->
      <el-card class="info-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>产品描述</span>
          </div>
        </template>
        <div class="product-description">
          <p v-if="product.description">{{ product.description }}</p>
          <p v-else class="no-description">暂无产品描述</p>
        </div>
      </el-card>

      <!-- 技术参数 -->
      <el-card class="info-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>技术参数</span>
          </div>
        </template>
        <div class="product-specs">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="spec-item">
                <label>材质成分：</label>
                <span>{{ product.material || '暂无' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="spec-item">
                <label>幅宽：</label>
                <span>{{ product.width || '暂无' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="spec-item">
                <label>克重：</label>
                <span>{{ product.weight || '暂无' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="spec-item">
                <label>密度：</label>
                <span>{{ product.density || '暂无' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="spec-item">
                <label>颜色：</label>
                <span>{{ product.color || '暂无' }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="spec-item">
                <label>产地：</label>
                <span>{{ product.origin || '暂无' }}</span>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-card>

      <!-- 销售统计 -->
      <el-card class="info-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>销售统计</span>
          </div>
        </template>
        <div class="sales-stats">
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="stat-item">
                <div class="stat-value">{{ product.sales || 0 }}</div>
                <div class="stat-label">总销量(米)</div>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="stat-item">
                <div class="stat-value">
                  ¥{{ ((product.sales || 0) * product.price).toFixed(2) }}
                </div>
                <div class="stat-label">销售额</div>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="stat-item">
                <div class="stat-value">{{ product.orderCount || 0 }}</div>
                <div class="stat-label">订单数量</div>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="stat-item">
                <div class="stat-value">{{ product.customerCount || 0 }}</div>
                <div class="stat-label">购买客户</div>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-card>

      <!-- 时间信息 -->
      <el-card class="info-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>时间信息</span>
          </div>
        </template>
        <div class="time-info">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="time-item">
                <label>创建时间：</label>
                <span>{{ formatDateTime(product.createTime) }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="time-item">
                <label>更新时间：</label>
                <span>{{ formatDateTime(product.updateTime) }}</span>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-card>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="handleEdit">
          <el-icon><Edit /></el-icon>
          编辑产品
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Picture, Edit } from '@element-plus/icons-vue'
import type { Product } from '@/types'

// 组件属性
interface Props {
  modelValue: boolean
  product: Product | null
}

// 组件事件
interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'edit', product: Product): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 对话框显示状态
const visible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit('update:modelValue', value),
})

/**
 * 获取产品类别名称
 * @param {string} category - 类别代码
 * @returns {string} 类别名称
 */
const getCategoryName = (category: string): string => {
  const categoryMap: Record<string, string> = {
    silk: '真丝面料',
    cotton: '棉质面料',
    wool: '羊毛面料',
    linen: '亚麻面料',
    polyester: '涤纶面料',
    blend: '混纺面料',
  }
  return categoryMap[category] || category
}

/**
 * 获取产品类别标签类型
 * @param {string} category - 类别代码
 * @returns {string} 标签类型
 */
const getCategoryTagType = (category: string): string => {
  const typeMap: Record<string, string> = {
    silk: 'danger',
    cotton: 'success',
    wool: 'warning',
    linen: 'info',
    polyester: 'primary',
    blend: '',
  }
  return typeMap[category] || ''
}

/**
 * 获取库存状态样式类
 * @param {number} stock - 库存数量
 * @returns {string} 样式类名
 */
const getStockClass = (stock: number): string => {
  if (stock <= 0) return 'stock-out'
  if (stock <= 100) return 'stock-low'
  return 'stock-normal'
}

/**
 * 格式化日期时间
 * @param {string} dateTime - 日期时间字符串
 * @returns {string} 格式化后的日期时间
 */
const formatDateTime = (dateTime: string): string => {
  if (!dateTime) return '暂无'
  return new Date(dateTime).toLocaleString('zh-CN')
}

/**
 * 处理关闭对话框
 */
const handleClose = () => {
  visible.value = false
}

/**
 * 处理编辑产品
 */
const handleEdit = () => {
  if (props.product) {
    emit('edit', props.product)
    handleClose()
  }
}
</script>

<style scoped>
.product-detail {
  max-height: 70vh;
  overflow-y: auto;
}

.info-card {
  margin-bottom: 16px;
}

.info-card:last-child {
  margin-bottom: 0;
}

.card-header {
  font-weight: 600;
  color: #303133;
}

.product-basic {
  display: flex;
  gap: 24px;
}

.product-image {
  flex-shrink: 0;
}

.image-slot {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 14px;
}

.product-info {
  flex: 1;
  min-width: 0;
}

.info-row {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  line-height: 1.5;
}

.info-row:last-child {
  margin-bottom: 0;
}

.info-row label {
  display: inline-block;
  width: 80px;
  font-weight: 500;
  color: #606266;
  flex-shrink: 0;
}

.info-row span {
  color: #303133;
}

.price {
  font-size: 16px;
  font-weight: 600;
  color: #e6a23c;
}

.stock-normal {
  color: #67c23a;
  font-weight: 500;
}

.stock-low {
  color: #e6a23c;
  font-weight: 500;
}

.stock-out {
  color: #f56c6c;
  font-weight: 500;
}

.product-description {
  line-height: 1.6;
  color: #606266;
}

.no-description {
  color: #c0c4cc;
  font-style: italic;
}

.spec-item {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  line-height: 1.5;
}

.spec-item label {
  display: inline-block;
  width: 80px;
  font-weight: 500;
  color: #606266;
  flex-shrink: 0;
}

.spec-item span {
  color: #303133;
}

.sales-stats {
  text-align: center;
}

.stat-item {
  padding: 16px;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  background: #fafafa;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: #409eff;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
}

.time-info {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 6px;
}

.time-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  line-height: 1.5;
}

.time-item:last-child {
  margin-bottom: 0;
}

.time-item label {
  display: inline-block;
  width: 80px;
  font-weight: 500;
  color: #606266;
  flex-shrink: 0;
}

.time-item span {
  color: #303133;
}

.dialog-footer {
  text-align: right;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .product-basic {
    flex-direction: column;
    gap: 16px;
  }

  .product-image {
    align-self: center;
  }

  .info-row,
  .spec-item,
  .time-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .info-row label,
  .spec-item label,
  .time-item label {
    width: auto;
  }
}
</style>
